package com.democode.auth.controller;

import com.demoCode.common.common.BaseResponse;
import com.demoCode.common.common.ErrorCode;
import com.demoCode.common.common.ResultUtils;
import com.demoCode.common.exception.BusinessException;
import com.demoCode.model.auth.dto.AuthLoginDto;
import com.demoCode.model.auth.dto.AuthRegisterDto;
import com.demoCode.model.auth.dto.CheckCaptchaDto;
import com.demoCode.model.auth.vo.AuthCaptchaVo;
import com.demoCode.model.auth.vo.AuthLoginVo;
import com.democode.auth.service.AuthUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping("/auth")
public class AuthUserController {

    @Resource
    private AuthUserService authUserService;

    /**
     * 用户登录
     * @param authLoginDto 登录参数
     * @return 用户登录返回信息
     */
    @PostMapping("/login")
    public BaseResponse<AuthLoginVo> login(@RequestBody @Valid AuthLoginDto authLoginDto) {
       if (authLoginDto == null){
           throw new BusinessException(ErrorCode.PARAMS_NOT_ERROR);
       }
       AuthLoginVo authLoginVo = authUserService.userLogin(authLoginDto);
       return ResultUtils.success(authLoginVo);
    }

    /**
     * 用户注册
     * @param authRegisterDto 注册参数
     * @return 注册结果
     */
    @PostMapping("/register")
    public Boolean register(@RequestBody @Valid AuthRegisterDto authRegisterDto) {
        if (authRegisterDto == null){
            throw new BusinessException(ErrorCode.PARAMS_NOT_ERROR);
        }
        return authUserService.userRegister(authRegisterDto);
    }

    /**
     * 退出登录
     * @return
     */
    @GetMapping("/logout")
    public Boolean logout(){
        return authUserService.userLogout();
    }

    /**
     * 获取验证码
     * @return
     */
    @GetMapping("/captcha")
    @ApiOperation("获取验证码")
    public BaseResponse<AuthCaptchaVo> getCaptcha(){
        AuthCaptchaVo authCaptchaVo =  authUserService.getCaptcha();
        return ResultUtils.success(authCaptchaVo);
    }

    /**
     * 校验验证码
     * @param checkCaptchaDto
     * @return
     */
    @PostMapping("/captcha/check")
    public Boolean checkCaptcha(@RequestBody @Valid CheckCaptchaDto checkCaptchaDto){
        return authUserService.checkCaptcha(checkCaptchaDto);
    }

}
